package com.demo.apparel.service.uniapp.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.demo.apparel.common.IdGenerator;
import com.demo.apparel.dto.uniapp.ShopRegisterDTO;
import com.demo.apparel.entity.Shop;
import com.demo.apparel.mapper.uniapp.ShopMapper;
import com.demo.apparel.service.uniapp.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class ShopServiceImpl implements ShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Override
    @Transactional
    public void registerShop(ShopRegisterDTO dto, String userId) {
        // 1. 检查是否已经入驻过
        Shop exist = shopMapper.selectOne(new QueryWrapper<Shop>().eq("user_id", userId));
        if (exist != null) {
            throw new RuntimeException("该用户已入驻商家，不可重复创建！");
        }

        // 2. 创建店铺
        Shop shop = new Shop();
        shop.setShopId(IdGenerator.createShopId());
        shop.setUserId(userId);
        shop.setShopName(dto.getShopName());
        shop.setDescription(dto.getDescription());
        shop.setCreatedTime(LocalDateTime.now());
        shop.setUpdatedTime(LocalDateTime.now());
        shop.setStatus(1);

        shopMapper.insert(shop);
    }

    @Override
    public Shop getShopByUserId(String userId) {
        return shopMapper.selectOne(new QueryWrapper<Shop>().eq("user_id", userId));
    }
}

